    package com.cykj.filter;

    import com.alibaba.fastjson.JSON;
    import com.cykj.dto.ResponseDTO;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.core.io.buffer.DataBuffer;
    import org.springframework.core.io.buffer.DataBufferUtils;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.server.reactive.ServerHttpRequest;
    import org.springframework.http.server.reactive.ServerHttpResponse;
    import org.springframework.web.server.ServerWebExchange;
    import reactor.core.publisher.Mono;
    import org.springframework.cloud.gateway.filter.GatewayFilterChain;
    import org.springframework.cloud.gateway.filter.GlobalFilter;
    import org.springframework.stereotype.Component;

    import java.util.List;

    @Component
    public class LoginFilter implements GlobalFilter {
        @Autowired
        private  RedisTemplate<String,Object> redisTemplate;
        //    @Autowired
    //    private RedisTemplate<String,String> redisTemplate;
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            ServerHttpRequest request = exchange.getRequest();
            ServerHttpResponse response = exchange.getResponse();
            response.getHeaders().add("Content-Type", "application/json;charset=utf-8");
    //        DataBuffer dataBuffer=null;
    //        try {
    //            dataBuffer=DataBuffer.class.newInstance();
    //           dataBuffer.write("hello".getBytes());
    //        } catch (InstantiationException e) {
    //            throw new RuntimeException(e);
    //        } catch (IllegalAccessException e) {
    //            throw new RuntimeException(e);
    //        }
    //        return Mono.just(null);
            String[] whiteList={"/login","/reg","/testConfig","index","/uLogin","/testGood"};
            System.out.println("进入过滤器");
            DataBuffer wrap= (DataBuffer) exchange.getResponse().bufferFactory().wrap("hello,拦截了".getBytes());
            String uri=request.getPath().toString();
            System.out.println(uri);
            for (String s : whiteList) {
                if(s.equals(uri)){
                    return chain.filter(exchange);
                }
            }
            redisTemplate.opsForValue().set("211","123456");

            List<String> strings=request.getHeaders().get("X-Token");

                    if(strings==null||strings.size()<=0){
                        response.setStatusCode(HttpStatus.UNAUTHORIZED);
                        ResponseDTO dto=ResponseDTO.error("Token不存在，请重新登录");
                         wrap=exchange.getResponse().bufferFactory().wrap(JSON.toJSONBytes(dto));
                        return exchange.getResponse().writeWith(Mono.just(wrap));
                    }
                    String token=strings.get(0);
            System.out.println(token+"这是");
            if(!redisTemplate.hasKey(token)){
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                ResponseDTO dto=ResponseDTO.error("Token不存在，请重新登录");
                wrap=exchange.getResponse().bufferFactory().wrap(JSON.toJSONBytes(dto));
                return exchange.getResponse().writeWith(Mono.just(wrap));
            }
            return exchange.getResponse().writeWith(Mono.just(wrap));
    //        return chain.filter(exchange);
        }
    }
